package com.blogsoho.taurus.services.impl;

import com.blogsoho.taurus.dao.mapper.UserMapper;
import com.blogsoho.taurus.services.UserService;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created by 16978 on 2017/6/15.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public ResponseWrapper<UserVO> getUserByName(String name) {
        ResponseWrapper<UserVO> response = new ResponseWrapper<>();
        UserVO user = userMapper.getUserByName(name);
        response.setData(user);
        return response;
    }

    @Override
    public ResponseWrapper<?> addUser(UserVO user) {
        ResponseWrapper<?> response = new ResponseWrapper<>();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        boolean result = userMapper.addUser(user) > 0 ? true : false;
        response.setSuccess(result);
        return response;
    }
}
